﻿using System;
using System.Text;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using LanceZhang.Research.BLL;
using LanceZhang.Research.Model;
using System.Security;
using System.Security.Cryptography;

public partial class UserControl_MyInfo : System.Web.UI.UserControl
{
    TBL_UserModel u = new TBL_UserModel();
    TBL_User uc = new TBL_User();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["id"] == null)//如果查看自己的信息
        {
            u = (TBL_UserModel)Session["UM"];
            BtnUpdate.Enabled = true;
            btnChangePW.Enabled = true;

        }
        else//如果查看他人信息
        {
            u = uc.GetModelByCache(Request["id"].ToString());
        }

        if (!IsPostBack)
        {
            dispUser(u);
        }
    }


    /// <summary>
    /// 显示用户的信息
    /// </summary>
    /// <param name="user">用户对象</param>
    private void dispUser(TBL_UserModel user)
    {
        this.txtBirthday.Text = user.Birthday.Date.ToString();
        this.txtClass.Text = user.ClassName;
        this.txtClassDate.Text = user.ClassAlterDate.Date.ToString();
        this.txtDegree.Text = user.Degree;
        this.txtMail.Text = user.Mail;
        this.txtTel.Text = user.Tel;
        this.txtTitle.Text = user.TitleName;
        this.txtTitleDate.Text = user.TitleAlterDate.Date.ToString();
        this.txtWorkTime.Text = user.Workday.Date.ToString();
        this.lblID.Text = user.UserID;
        this.lblName.Text = user.UserName;
        this.lblState.Text=((user.Status)?"在职":"离职");
        if (user.Sex)
        {
            this.RadioButtonList1.SelectedIndex = 0;
        }
        else
        {
            this.RadioButtonList1.SelectedIndex = 1;
        }
    }

    private void canedit(bool yn)
    {
        this.txtBirthday.Enabled = yn;
        this.txtClass.Enabled = yn;
        this.txtClassDate.Enabled = yn;
        this.txtDegree.Enabled = yn;
        this.txtMail.Enabled = yn;
        this.txtTel.Enabled = yn;
        this.txtTitle.Enabled = yn;
        this.txtTitleDate.Enabled = yn;
        this.txtWorkTime.Enabled = yn;
        this.RadioButtonList1.Enabled = yn;
        this.BtnOk.Enabled = yn;
    }



    protected void BtnUpdate_Click(object sender, EventArgs e)
    {
        this.BtnUpdate.Enabled = false;
        this.canedit(true);
    }
    protected void BtnOk_Click(object sender, EventArgs e)
    {
        try
        {
            u.Birthday = Convert.ToDateTime(this.txtBirthday.Text);
            u.ClassAlterDate = Convert.ToDateTime(this.txtClassDate.Text);
            u.ClassName = this.txtClass.Text;
            u.Degree = this.txtDegree.Text;
            u.Mail = this.txtMail.Text;
            u.Sex = (this.RadioButtonList1.SelectedIndex == 1) ? true : false;
            u.Tel = this.txtTel.Text;
            u.TitleAlterDate = Convert.ToDateTime(this.txtTitleDate.Text);
            u.TitleName = this.txtTitle.Text;
            u.Workday = Convert.ToDateTime(this.txtWorkTime.Text);
        }
        catch (Exception ex)
        {
            Page.RegisterStartupScript("warnings", "<script>alert('日期输入时间不对！');</script>");
            return;
        }
        uc.Update(u);
        Page.RegisterStartupScript("warnings", "<script>alert('修改成功！');</script>");
        this.BtnUpdate.Enabled = true;
        this.canedit(false);
    }
    protected void btnChangePW_Click(object sender, EventArgs e)
    {
        Panel1.Visible = true;
        this.btnChangePW.Enabled = false;
    }
    protected void btnChange_Click(object sender, EventArgs e)
    {
        if (this.txtNewPW.Text != this.txtConPW.Text)
        {
            Page.RegisterStartupScript("warnings", "<script>alert('新密码两次输入不一致！');</script>");
            return;
        }
        byte[] EncodePwd = new byte[20];
        EncodePwd=Encoding(this.txtOldPW.Text);
        if (!Compare(EncodePwd,u.Password))
        {
            Page.RegisterStartupScript("warnings", "<script>alert('原密码错误！');</script>");
            return;
        }
        try
        {
            EncodePwd = Encoding(this.txtNewPW.Text);
            u.Password = EncodePwd;
            uc.Update(u);
            Page.RegisterStartupScript("warnings", "<script>alert('修改成功！');</script>");
            Panel1.Visible = false;
            this.btnChangePW.Enabled = true;
        }
        catch
        {
            Page.RegisterStartupScript("warnings", "<script>alert('修改失败！');</script>");
        }
    }




    /// <summary>
    /// 计算输入密码的hash值
    /// </summary>
    /// <param name="chars">EncodePwd</param>
    /// <returns></returns>
    private byte[] Encoding(string chars)
    {
        //采用了SHAI算法计算输入密码的hash值
        SHA1Managed mydata = new SHA1Managed();
        byte[] bites = new byte[20];
        ASCIIEncoding pw = new ASCIIEncoding();
        bites = pw.GetBytes(chars);
        bites = mydata.ComputeHash(bites);
        return bites;
    }

    /// <summary>
    /// 匹配密码
    /// </summary>
    /// <param name="b1"></param>
    /// <param name="b2"></param>
    /// <returns>是否正确</returns>
    private bool Compare(byte[] b1, byte[] b2)
    {
        for (int i = 0; i < 19; i++)
        {
            if (b1[i] != b2[i])
                return false;
        }
        return true;
    }
}
